﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using SQLServerHealthCheck.Model;
using AG.Utilities;

namespace SQLServerHealthCheck.SQLServer.IssueTypes
{
    public class ServerCheckFreeSpaceLess10percent : IssueTypeCheckBase
    {
        public ServerCheckFreeSpaceLess10percent()
        {
            IssueTypeId = 1001;
        }

        public override void Check()
        {
            try
            {
                HealthCheckDataContext dc = new HealthCheckDataContext(System.Configuration.ConfigurationManager.AppSettings["ConnectionStringSSHCDatabase"]);
                Issue issue;
                ThresholdValue thresholdValue = dc.ThresholdValues.FirstOrDefault(t => t.IssueTypeID == IssueTypeId && t.ThresholdValueID == 1);
                var ServerQry = from sd in dc.Servers
                                from d in sd.ServerDrives
                         
                                where (Convert.ToDouble(((d.FreeSpaceInGB ) / d.TotalSpaceInGB)) < Convert.ToDouble((thresholdValue.Value/100.0)))


                                select d;
                
                foreach (var item in ServerQry)
                {
                   
                    //Get Issuetype object

                    //Get Issuetype object  And Create a new  Global issue object 

                    IssueType issueType = dc.IssueTypes.FirstOrDefault(i => i.IssueTypeId == IssueTypeId);
       
                    issue = new Issue();

                    issue.IssueType = issueType;
                    issue.TargetId = item.ServerID;
                    issue.Description = issueType.Description.Replace("{ServerName}", item.Server.Name).Replace("{Drive}", item.Drive.ToString());

                    dc.Issues.InsertOnSubmit(issue);
                }

                dc.SubmitChanges();
            }
            catch (Exception ex)
            {
                Logger.Error(ex);
            }

        }


    }








}
